<?php
class Auth_model extends CI_Model {
    public function __construct() {
        parent::__construct();
    }
    public function login() {
        $flow = 'nok';
        $this->load->database();
        $barangay_name = $this->input->post('barangay_name');
        $municipality = $this->input->post('municipality');
        $province = $this->input->post('province');
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $check_user = $this->db->query("
            SELECT a.user_id_pk AS user_id
            ,a.user_username AS username
            , b.barangay_id_pk AS barangay_id
            , b.barangay_name AS barangay
            , c.municipality_name AS municipality
            , d.province_name AS province
            , b.barangay_zip AS zip
            , b.barangay_logo_path AS logo
            , b.barangay_phone AS phone
            , b.barangay_email AS email
            , year(b.barangay_created) as creation
            , b.barangay_status AS status
            FROM tb_users a, tb_barangays b, tb_municipalities c, tb_provinces d
            WHERE a.user_username='".$username."' AND
            a.user_password=PASSWORD('".$password."') AND 
            b.barangay_name='".$barangay_name."' AND
            c.municipality_name='".$municipality."' AND
            d.province_name='".$province."' AND
            a.user_barangay_id_fk=b.barangay_id_pk AND
            b.barangay_municipality_id_fk=c.municipality_id_pk AND
            b.barangay_province_id_fk=d.province_id_pk AND
            b.barangay_status=2
        ");
       if($check_user->num_rows() == 0) {
           $flow = 'username not found';
           return $flow;
       }
       foreach($check_user->result() as $row) {
           if($row->status != 2) {
               $flow = 'barangay not activated';
               return $flow;
           }
           $this->session_setter('user_id', $row->user_id);
           $this->session_setter('username', $row->username);
           $this->session_setter('barangay_id', $row->barangay_id);
           $this->session_setter('barangay', $row->barangay);
           $this->session_setter('municipality', $row->municipality);
           $this->session_setter('province', $row->province);
           $this->session_setter('zip', $row->zip);
           $this->session_setter('logo', $row->logo);
           $this->session_setter('phone', $row->phone);
           $this->session_setter('email', $row->email);
           $this->session_setter('creation', $row->creation);
           $this->session_setter('user_resp', 'admin');
        }
        $get_captain = $this->db->query("SELECT * from tb_officials a
                where a.official_position_id_fk=1 and a.official_barangay_id_fk=" . $this->session->userdata('barangay_id'));
        foreach($get_captain->result() as $cap) {
                $this->session_setter('barangay_captain', 'Hon. ' 
                        . $cap->official_firstname . ' ' . $cap->official_middlename . ' ' . $cap->official_lastname);
        }
        $flow = 'ok';
        return $flow;
    }
    public function logout() {
        $this->Auth_model->session_unsetter('barangay_id');
        $this->Auth_model->session_unsetter('barangay');
        $this->Auth_model->session_unsetter('municipality');
        $this->Auth_model->session_unsetter('province');
        $this->Auth_model->session_unsetter('logo');
        $this->Auth_model->session_unsetter('phone');
        $this->Auth_model->session_unsetter('email');
        $this->Auth_model->session_unsetter('creation');
        $this->Auth_model->session_unsetter('barangay_captain');
        $this->Auth_model->session_unsetter('user_resp');
        if($this->session->userdata('user_resp') == "admin") {
            $this->Auth_model->session_unsetter('user_resp');
            $this->Auth_model->session_unsetter('user_id');
        } else {
            $this->Auth_model->session_unsetter('user_resp');
        }
    }
    public function changePassword() {
        $this->load->database();
        $query = $this->db->query("
            UPDATE  tb_users SET  user_password =  PASSWORD('".$this->input->post('pwd')."') 
            WHERE  user_id_pk = ".$this->session->userdata('user_id')."  AND
            user_barangay_id_fk = ".$this->session->userdata('barangay_id')."
            LIMIT 1 
        ");
    }
    public function viewSite() {
        $flow = 'nok';
        $this->load->database();
        $barangay = $this->input->post('barangay_val');
        $municipality = $this->input->post('municipality_val');
        $province = $this->input->post('province_val');
        $brgy_query = $this->db->query("SELECT b.barangay_id_pk AS barangay_id, b.barangay_name AS barangay, c.municipality_name AS municipality, d.province_name AS province, b.barangay_logo_path AS logo, b.barangay_phone AS phone, b.barangay_email AS email, YEAR( b.barangay_created ) AS creation, b.barangay_status AS 
            status 
            FROM tb_users a, tb_barangays b, tb_municipalities c, tb_provinces d
            WHERE b.barangay_id_pk =1
            AND b.barangay_municipality_id_fk =1
            AND b.barangay_province_id_fk =1
            AND b.barangay_municipality_id_fk = c.municipality_id_pk
            AND b.barangay_province_id_fk = d.province_id_pk
            AND b.barangay_status=2");
        if($brgy_query->num_rows() == 0) {
           $flow = 'barangay not found';
           return $flow;
       }
       foreach($brgy_query->result() as $row) {
           if($row->status != 2) {
               $flow = 'barangay not yet registered or not activated';
               return $flow;
           }
           $this->session_setter('barangay_id', $row->barangay_id);
           $this->session_setter('barangay', $row->barangay);
           $this->session_setter('municipality', $row->municipality);
           $this->session_setter('province', $row->province);
           $this->session_setter('logo', $row->logo);
           $this->session_setter('phone', $row->phone);
           $this->session_setter('email', $row->email);
           $this->session_setter('creation', $row->creation);
           $this->session_setter('user_resp', 'viewer');
        }
        $get_captain = $this->db->query("SELECT * from tb_officials a
                where a.official_position_id_fk=1 and a.official_barangay_id_fk=" . $this->session->userdata('barangay_id'));
        foreach($get_captain->result() as $cap) {
                $this->session_setter('barangay_captain', 'Hon. ' 
                        . $cap->official_firstname . ' ' . $cap->official_middlename . ' ' . $cap->official_lastname);
        }
        $flow = 'ok';
        return $flow;
        
    }
    private function session_setter($sess_name, $sess_value) {
        $this->session->set_userdata($sess_name, $sess_value);
    }
    private function session_unsetter($sess_name) {
        $this->session->unset_userdata($sess_name);
    }
}